package com.gitee.srurl.core.event;

import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zxw
 * @date 2023/5/2 14:53
 */
@Slf4j
public class EventBusHandle {

    /**
     * eventBus 目前设置的是同步的 如果有需要可以改成异步的
     */
    private static final EventBus eventBus = new EventBus();

    public static void post(EventParams event) {
        eventBus.post(event);
    }

    /**
     * 注册事件
     */
    public static void register(Object object, String type) {
        try {
            eventBus.register(object);
            log.info("注册事件:{}，成功", type);
        } catch (Exception e) {
            log.error("注册事件:{}，失败,ex:", type, e);
        }
    }

    /**
     * 取消注册
     */
    public static void unregister(Object object, String type) {
        try {
            eventBus.unregister(object);
            log.info("取消注册事件:{}，成功", type);
        } catch (Exception e) {
            log.info("取消注册事件:{}，失败, ex:", type, e);
        }
    }
}
